package com.burgeon.backend.SHOPBACKEND.rest.invite;
/**
 * 邀约记录
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.invite.Invite;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class QuerryInviteData extends RESTBase {
    private final static int RANGE = 20;//分页数量
    private String ip;
    private String account;
    private String password;

    public QuerryInviteData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public HashMap getData(String type,int start) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(type,start), url);
        System.err.println(str);
        String code = new JSONArray(str.replace("null","-")).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        ArrayList<Invite> invites = new ArrayList<Invite>();
        JSONArray jsonArray = new JSONArray(str.replace("null","-").replace("N/A","-")).getJSONObject(0).getJSONArray("rows");

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);
            Invite invite = new Invite();
            invite.setId(array.getString(0));
            invite.setGoodsCode(array.getString(1));
            invite.setType(array.getString(2));
            invite.setBillDate(array.getString(3));
            invite.setVipName(array.getString(4));
            invite.setVipPhone(array.getString(5));
            invite.setEmpName(array.getString(6));
            invite.setStore(array.getString(7));
            invite.setVisitType(array.getString(8));
            invite.setContent(array.getString(9));
            invite.setVipFeedBack(array.getString(10));
            invite.setFeedBack(array.getString(11));
            invite.setEmpId(array.getString(12));

            invites.add(invite);
        }
        HashMap data = new HashMap();
        data.put("list",invites);
        data.put("count",count);
        return data;
    }

    private MultiValueMap Data(String type, int start) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("'M_PRODUCT_ID;NAME'");
        columns.add("BILLTYPE");//邀约类型
        columns.add("BILLDATE");
        columns.add("'C_VIP_ID;VIPNAME'");//会员
        columns.add("'C_VIP_ID;CARDNO'");//会员卡号
        columns.add("'HR_EMPLOYEE_ID;NAME'");//员工
        columns.add("'C_STORE_ID;NAME'");//店名
        columns.add("VISITTYPE");//回访方式 1:QQ,2:微信,3:电话,4:钉钉,5:短信,
        columns.add("RVISIT_CONTENT");//回访内容
        columns.add("'VIP_FEEDBACK'");//客户反馈
        columns.add("FEEDBACK");//邀约反馈 1:承诺到店,2:确定到店,3:拒绝到店,4:不确定,
        columns.add("'HR_EMPLOYEE_ID'");//员工id


        params.put("columns", columns);
        params.put("table", "C_VIP_APPRVISIT");//表名

        params.put("count", "true");

        params.put("range", RANGE);

        params.put("start", String.valueOf(start * RANGE));

        //设置排序规则
        HashMap orderby = new HashMap();
        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();//DOCNO  (AK)	单据编号
        String ac = "'"+type+"'";
        paramss.put("column", "'BILLTYPE'");
        paramss.put("condition", ac);
        //params.put("range",1000);
        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        QuerryInviteData querryYCDisStatus = new QuerryInviteData("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(querryYCDisStatus.getData("4",0));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
